<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('进度日期')" prop="progressDate">
            <el-date-picker
              v-model="form.progressDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('进度日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('录入人')" prop="dataEntryClerk">
            <UserSelect v-model="form.dataEntryClerk" :placeholder="$t('录入人')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('已支付金额') + $t('金额单位')" prop="theAmountPaid">
            <Money v-model="form.theAmountPaid" :placeholder="$t('已支付金额') + $t('金额单位')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('预估当前产值') + $t('金额单位')" prop="estimatedOutputValue">
            <Money v-model="form.estimatedOutputValue" :placeholder="$t('预估当前产值') + $t('金额单位')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('进度说明')" prop="progressThat">
            <el-input v-model="form.progressThat" type="textarea" :placeholder="$t('进度说明')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="progress_tracking" file-title="施工图片附件" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:progressTracking" />
    </div>
  </div>
</template>

<script>
    import { saveDealProgressTracking, getDealProgressTracking, gainTheAmountPaidByDealId } from '@/api/deal/progressTracking/dealProgressTracking'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                  // 上传
                  fileList: []
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                progressDate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                dataEntryClerk: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                theAmountPaid: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                estimatedOutputValue: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ]
                }
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealProgressTracking(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    // 获取已支付金额
                  gainTheAmountPaidByDealId(this.$route.query.id, null, null).then(response => {
                    this.form.theAmountPaid = response.data
                  })
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    progressDate: undefined,
                    dataEntryClerk: this.$store.state.user.userId,
                    theAmountPaid: undefined,
                    estimatedOutputValue: undefined,
                    progressThat: undefined,
                    delFlag: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealProgressTracking(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           }
        }
    }
</script>
